var express = require('express'),
    router = express.Router(),
    swig = require('swig-templates'),
    request = require('request'),
    common = require('../base/common.js'),
    loginAuth = require('../base/login-auth.js');
router.get('/',function(req, res,next) {
    loginAuth.loginAuth(req, res, function () {
        var thatData = {
                access_token: req.cookies.access_token,
                EquipmentsListIdList:[]
            },
            u_id = req.cookies.u_id ? req.cookies.u_id : '';
    function getUsetServiceList(callback) {
        request.post({
            url: config.resourcePath.apiRoot + "/index.php?m=PersonalCenter&a=getUserServiceList",
            formData: {
                u_id: u_id,
                phone_number: req.cookies.phone_number?req.cookies.phone_number:'',
                limit: 1000,
                stype:'T02'
            }
        }, function (Serror, Sresponse, Sbody) {
            try {
                var ServiceList = JSON.parse(Sbody);
                console.log('ServiceList');
                console.log(ServiceList);
                thatData.ServiceList = ServiceList;
                if(callback) callback();
            } catch (e) {
                if(callback) callback();
            }
        });
    }
    function getUserEquipmentsList(callback) {
        request.post({
            url: config.resourcePath.apiRoot + "/index.php?m=PersonalCenter&a=getUserEquipmentsList",
            formData: {
                u_id: u_id
            }
        }, function (Eerror, Eresponse, Ebody) {
            try {
                var EquipmentsList = JSON.parse(Ebody);
                if(EquipmentsList.data && EquipmentsList.data.equipments.length>0){
                    for(var i in EquipmentsList.data.equipments){
                        thatData.EquipmentsListIdList.push(EquipmentsList.data.equipments[i].id);
                    }
                }
                thatData.EquipmentsList = EquipmentsList;
                if(callback) callback();
            } catch (e) {
                if(callback) callback();
            }
        });
    }

    function getUserInfo(callback) {
        request.post({
            url: config.resourcePath.apiRoot + "/index.php?m=personalCenter&a=getUserInfo",
            formData: {
                u_id: u_id
            }
        }, function (error, response, body) {
            try {
                var result = JSON.parse(body);
                // console.log(result);
                thatData.User = result;
                if (thatData.User.userInfo.username) {
                    thatData.User.userInfo.username = req.cookies.username;
                }
                if(callback) callback();
                res.render('personal-center/personal-center', thatData);
            } catch (e) {
                if(callback) callback();
            }
        });
    }
    getUsetServiceList(function () {
        getUserEquipmentsList(function () {
            getUserInfo();
        });
    });
    });
});

// 反序
swig.setFilter('EquipmentsList', function (arr,id) {
    var stats=false;
    for(var i in arr){
        if(id==arr[i]){
            stats=true;
            break;
        }
    }
    return stats;
});
router.get('/2r',function(req, res,next) {
    res.render('personal-center/personal-center-2r/personal-center-2r');
});
module.exports = router;
